home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-05-08 | 5.2 KB | 256 lines | [TEXT/MMCC] |
- /*
- * TRunOSA.cp
- *
- * Implementation for 'TRunOSA', a C++
- * class to manipulate AppleScripts
- *
- * azn@nemeng.mpx.com.au
- *
- * © Andrew Nemeth Warrimoo Australia 1995
- */
-
-
- #include "TRunOSA.h"
-
- // #define NDEBUG
- #include <assert.h>
-
- #include <OSAGeneric.h>
- #include <Components.h>
- #include <OSA.h>
- #include <AppleScript.h>
- #include <AEObjects.h>
- #include <Gestalt.h>
-
-
- // Class static variable definition
- //
- ComponentInstance TRunOSA::scriptComponent = NULL;
-
-
-
-
- TRunOSA::TRunOSA( )
- : f_osaidOriginal ( kOSANullScript ),
- f_osaidResult ( kOSANullScript )
- {
- f_aedescScript.descriptorType = typeNull;
- f_aedescScript.dataHandle = NULL;
- }
-
-
-
-
- TRunOSA::~TRunOSA()
- {
- if ( NULL != TRunOSA::scriptComponent )
- {
- ::AEDisposeDesc( &f_aedescScript );
- ::OSADispose( TRunOSA::scriptComponent, f_osaidOriginal );
- ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
- }
- }
-
-
-
-
- OSAError TRunOSA::initOSA( void )
- {
- Handle hScript = NULL;
- OSAError osaErr = noErr;
-
- hScript = ::Get1IndResource( typeOSAGenericStorage, 1 );
- if ( NULL == hScript )
- return( resNotFound );
-
- ::DetachResource( hScript );
-
- osaErr = myInitOSA( hScript );
-
- return( osaErr );
- }
-
-
-
-
- OSAError TRunOSA::initOSA( const FSSpec & fsspecScript )
- {
- Handle hScript = NULL;
- short shResRefNum = -1;
- OSAError osaErr = noErr;
-
- shResRefNum = ::FSpOpenResFile( &fsspecScript, fsRdPerm );
- if ( -1 == shResRefNum )
- return( ::ResError() );
-
- hScript = ::Get1IndResource( typeOSAGenericStorage, 1 );
- if ( NULL == hScript )
- return( resNotFound );
-
- ::DetachResource( hScript );
- ::CloseResFile( shResRefNum );
-
- osaErr = myInitOSA( hScript );
-
- return( osaErr );
- }
-
-
-
-
- OSAError TRunOSA::myInitOSA( Handle hScript )
- {
- const long klgGestaltMask = 1L;
- long lgFeature = 0L;
- AEDesc aedescDummy = { typeNull, NULL };
- OSErr myErr = noErr;
- OSAError osaErr = noErr;
-
- assert( NULL != hScript );
-
- if ( NULL == TRunOSA::scriptComponent )
- {
- myErr = ::Gestalt( gestaltAppleEventsAttr, &lgFeature );
- if ( ( noErr == myErr ) &&
- ( lgFeature & ( klgGestaltMask << gestaltScriptingSupport ) ) )
- NULL;
- else
- return( errOSACantOpenComponent );
-
- TRunOSA::scriptComponent =
- ::OpenDefaultComponent( kOSAComponentType,
- kOSAGenericScriptingComponentSubtype );
-
- osaErr = ::OSASetDefaultScriptingComponent( TRunOSA::scriptComponent,
- kAppleScriptSubtype );
- if ( noErr != osaErr )
- return( osaErr );
- }
-
- ::AEDisposeDesc( &f_aedescScript );
- f_aedescScript.descriptorType = typeOSAGenericStorage;
- f_aedescScript.dataHandle = hScript;
-
- if ( kOSANullScript != f_osaidOriginal )
- ::OSADispose( TRunOSA::scriptComponent, f_osaidOriginal );
-
- osaErr = ::OSALoad( TRunOSA::scriptComponent, &f_aedescScript,
- kOSAModeNull, &f_osaidOriginal );
-
- ::OSADisplay( TRunOSA::scriptComponent, f_osaidOriginal,
- typeChar, kOSAModeDisplayForHumans,
- &aedescDummy );
-
- ::AEDisposeDesc( &aedescDummy );
-
- return( osaErr );
- }
-
-
-
-
- OSAError TRunOSA::runScript( void )
- {
- if ( NULL == TRunOSA::scriptComponent )
- return( errOSAInvalidID );
-
- if ( kOSANullScript != f_osaidResult )
- ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
-
- return( ::OSAExecute( TRunOSA::scriptComponent, f_osaidOriginal,
- kOSANullScript, kOSAModeNull, &f_osaidResult ) );
- }
-
-
-
-
- OSAError TRunOSA::runScript( const AEEventClass aeclassSuite,
- const AEEventID aesuiteKind,
- Str255 str255Var )
- {
- AEDesc aedescVar = { typeNull, NULL };
- OSAError osaErr = noErr;
-
- assert( str255Var[0] > 0 );
-
- osaErr = ::AECreateDesc( typeChar, &str255Var[1],
- str255Var[0], &aedescVar );
-
- if ( noErr == osaErr )
- osaErr = myRunScript( aeclassSuite, aesuiteKind, &aedescVar );
-
- ::AEDisposeDesc( &aedescVar );
-
- return( osaErr );
- }
-
-
-
-
- OSAError TRunOSA::myRunScript( const AEEventClass aeclassSuite,
- const AEEventID aesuiteKind,
- AEDesc * ptraedescVar )
- {
- AppleEvent aeEvent = { typeNull, NULL };
- OSAError osaErr = noErr;
-
- if ( NULL == TRunOSA::scriptComponent )
- return( errOSAInvalidID );
-
- assert( aeclassSuite > 0L );
- assert( aesuiteKind > 0L );
-
- osaErr = ::AECreateAppleEvent( aeclassSuite,
- aesuiteKind,
- &f_aedescScript,
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &aeEvent );
-
- if ( NULL != ptraedescVar && noErr == osaErr )
- osaErr = ::AEPutParamDesc( &aeEvent, keyDirectObject, ptraedescVar );
-
- if ( noErr == osaErr && ( kOSANullScript != f_osaidResult ) )
- ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
-
- if ( noErr == osaErr )
- osaErr = ::OSAExecuteEvent( TRunOSA::scriptComponent,
- &aeEvent, f_osaidOriginal,
- kOSAModeNull, &f_osaidResult );
-
- ::AEDisposeDesc( &aeEvent );
-
- return( osaErr );
- }
-
-
-
-
- OSAError TRunOSA::getResultDesc( AEDesc * ptraedescResult,
- long * ptrlgSize )
- {
- OSAError osaErr = noErr;
-
- if ( NULL == TRunOSA::scriptComponent &&
- kOSANullScript == f_osaidResult )
- return( errOSAInvalidID );
-
- assert( NULL != ptraedescResult );
- assert( NULL != ptrlgSize );
-
- osaErr = ::OSADisplay( TRunOSA::scriptComponent, f_osaidResult,
- typeChar, kOSAModeDisplayForHumans,
- ptraedescResult );
-
- if ( noErr == osaErr )
- *ptrlgSize = ::GetHandleSize( ptraedescResult->dataHandle );
- else
- *ptrlgSize = 0L;
-
- ::OSADispose( TRunOSA::scriptComponent, f_osaidResult );
- f_osaidResult = kOSANullScript;
-
- return( osaErr );
- }
-
-